package org.wdh.ep.sct.bf;
import java.util.NoSuchElementException;

public class LinkedList {
	private Object first;
	private LinkedList rest;
	public LinkedList() {
		this.first = null;
		this.rest = null;
	}
	public LinkedList(Object _first) {
		this.first = _first;
	}
	public LinkedList(Object _first, LinkedList _rest) {
		this.first = _first;
		this.rest = _rest;
	}
	public Object getFirst() {
		if(!isEmpty()) {
			return first;
		}
		else {
			throw new NoSuchElementException();
		}
	}
	public LinkedList getRest() {
		if(!isEmpty()) {
			return rest;
		}
		else {
			throw new NoSuchElementException();
		}
	}
	public void addFirst(Object o) {
		LinkedList oldThis = (LinkedList)this.clone();
	    this.first = o;
	    this.rest = oldThis;
	}
	public boolean isEmpty() {
		return this.first == null && this.rest == null;
	}
	public Object clone() {
		return new LinkedList(this.first, this.rest);
	}
	public boolean equals(Object that) {
		if (this.getClass() == that.getClass()) {
			 LinkedList _that = (LinkedList)that;
			 if (this.isEmpty() || _that.isEmpty()) {
				 return this.isEmpty() && _that.isEmpty();
			 }
			 else {
			    boolean firstEltsMatch = this.getFirst().equals(_that.getFirst());
			    boolean restEltsMatch = this.getRest().equals(_that.getRest());
			    return firstEltsMatch && restEltsMatch;
			 }
		}
		else {
			return false;
		}
	}
}
